package com.xing.sbmongodb.util;

import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.bson.json.JsonObject;
import org.omg.CORBA.PUBLIC_MEMBER;

import java.util.HashMap;
import java.util.Map;

import static com.fasterxml.jackson.databind.type.LogicalType.Map;

@Data
@NoArgsConstructor
//定义一个jsonResult类，用于封装接口返回数据
public class JsonResult {

    /**
     * 未登录
     */
    public static final int NO_LOGIN=400;
    /**
     * 登录失败
     */
    public static final int LOGIN_FAITED=401;
    /**
     * token过期
     */
    public static final int TOKEN_EXPIRED=402;
    /**
     * 无权限
     */
    public static final int NO_PERMISSION=403;

    private Boolean success;
    private Integer code;
    private String msg;
    private Object data;

    public JsonResult(Boolean success) {
        this.success = success;
    }

    public JsonResult(Boolean success, String msg) {
        this.success = success;
        this.msg = msg;
    }

    public JsonResult(Boolean success, Integer code, String msg) {
        this.success = success;
        this.code = code;
        this.msg = msg;
    }

    public JsonResult(Boolean success, Object data) {
        this.success = success;
        this.data = data;
    }



    public JsonResult(Boolean success, Integer code, String msg, Object data) {
        this.success = success;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public void put(String key,Object values){
        if(data==null){
            data=new HashMap<>();
        }
        ((Map)data).put(key,values);
    }

    public void putAll(Map<String ,Override> map){
        if(data==null){
            data=new HashMap<>();
        }
        ((Map)data).putAll(map);
    }

    @Override
    public String toString() {
        String jsonString = JSONObject.toJSONString(this);
        return jsonString;
    }
}
